<?php

namespace addons\mental\controller;

use addons\mental\model\Feedback as feedbackModel;
use app\admin\model\xlzxinfo\Xlzxinfo;
use think\Log;

class Im extends Base
{
    protected $noNeedLogin = ['*'];



    /**
     *
     * IM消息回调
     * @return void
     */
    public function imcallback()
    {
        $param = $this->request->post();
        Log::info('接收回调参数');
        Log::info($param);
//        $param = array (
//            'CallbackCommand' => 'C2C.CallbackAfterSendMsg',
//            'ClientIP' => '1.192.246.66',
//            'OptPlatform' => 'Web',
//            'RequestId' => '114598249-144115243229377664-1692598114-77585734',
//            'SdkAppid' => '1400793966',
//            'contenttype' => 'json',
//            'MsgBody' =>
//                array (
//                    0 =>
//                        array (
//                            'MsgType' => 'TIMTextElem',
//                            'MsgContent' =>
//                                array (
//                                    'Text' => '睡觉就睡觉',
//                                ),
//                        ),
//                ),
//            'From_Account' => '29',
//            'To_Account' => '72',
//            'MsgRandom' => '77585734',
//            'MsgSeq' => '1408340001',
//            'MsgTime' => '1692598114',
//            'SupportMessageExtension' => '0',
//            'MsgKey' => '1408340001_77585734_1692598114',
//            'OnlineOnlyFlag' => '0',
//            'SendMsgResult' => '0',
//            'ErrorInfo' => 'send msg succeed',
//            'UnreadMsgNum' => '9',
//            'addon' => 'mental',
//            'controller' => 'im',
//            'action' => 'imcallback',
//        );
        $To_Account = $param['To_Account'];
        $From_Account = $param['From_Account'];
        $touser = \app\common\model\User::where('id',$To_Account)->find();
        $fromuser = \app\common\model\User::where('id',$From_Account)->find();
        $text = $param['MsgBody'][0]['MsgContent']['Text'];
        //发送公众号 IM消息模板推送
        Log::info('关注公众号official_openid:'.$touser['official_openid']);
        if ($touser && $fromuser && !empty($touser['official_openid'])){
            $xlzxinfo = Xlzxinfo::where(['user_id'=>$touser['id']])->find();
            if ($xlzxinfo){
                $touser['full_name'] = $xlzxinfo['full_name'];
//                var_dump($touser['full_name']);
//                die;
                \addons\mental\model\WxUser::push_template_im($touser,$fromuser,$text);
            }

        }
        $this->success();
    }
}
